package com.mojo.collection.map;

import java.util.HashMap;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author <a href="mailto:sjj@jianzhimao.com">mojo</a>
 * copyright (C), 2013-2022, 广州九尾信息科技有限公司
 * @date 2022/3/15 9:56
 */
public class ConcurrentHashMapDemo {
    private static HashMap<Integer, String> map = new HashMap<Integer, String>(2, 0.75f);

    public static void main(String[] args) {
        ConcurrentHashMap<String, Object> map1 = new ConcurrentHashMap<>();
        //int n = 0;
        //for (int i = 0; i < 10; i++) {
        //    new Thread(new Runnable1(n)).start();
        //}
        //Thread.yield();
        //System.out.println(">>>>" + n);
        for (int i = 0; i < 10; i++) {

            ConcurrentModifier();
        }
    }

    private static void ConcurrentModifier() {
        map.put(5, "C");

        new Thread("Thread1") {
            @Override
            public void run() {
                map.put(7, "B");
                System.out.println(map);
            }
        }.start();
        new Thread("Thread2") {
            public void run() {
                map.put(3, "A");
                System.out.println(map);
            }

        }.start();
        System.out.println(map.get(7));
    }
}

class Runnable1 implements Runnable {
    Integer n;

    public Runnable1(Integer n) {
        this.n = n;
    }

    @Override
    public void run() {
        n++;
        System.out.println("线程" + Thread.currentThread().getName());
    }
}